package com.whcoding.base.project.controller;

import cn.hutool.core.util.StrUtil;
import com.whcoding.base.project.common.BasicsPageUtil;
import com.whcoding.base.project.common.HutoolPageUtil;
import com.whcoding.base.project.common.api.BasePageResult;
import com.whcoding.base.project.common.api.BaseResultVO;
import com.whcoding.base.project.pojo.OrmDeptCondition;
import com.whcoding.base.project.pojo.OrmDeptVO;
import com.whcoding.base.project.pojo.PageParamVO;
import com.whcoding.base.project.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: spring-boot-learning
 * @description:http://172.16.57.141:9800/basic_project/swagger-ui.html#/
 * @author: whcoding
 * @create: 2022-05-26 14:17
 **/
@RestController
@RequestMapping("/deptTemp/")
@Api(tags = "获取部门信息", description = "部门信息的CRUD")
@Validated
public class DeptRESTfulController {

	private static final Logger log = LoggerFactory.getLogger(DeptController.class);

	@Autowired
	private DeptService deptService;

	/**
	 * 程序健康检查
	 *	@NotControllerResponseAdvice 返回的就是字符串
	 * http://172.16.57.141:9800/basic_project/deptTemp/health
	 * @return
	 */
	@GetMapping("/health")
	public String health() {
		return "success";
	}

	/**
	 * 保存数据并使用 Validated 验证参数
	 *
	 * @param deptCondition
	 * @return
	 */
	@PostMapping("save")
	@ApiOperation("/保存数据")
	public BaseResultVO saveDept(@RequestBody @Validated OrmDeptCondition deptCondition) {
		OrmDeptVO ormDeptVO = new OrmDeptVO();
		BeanUtils.copyProperties(deptCondition, ormDeptVO);
		log.info("转换数据执行完成！");
		boolean isSave = deptService.addDept(ormDeptVO);
		if (isSave) {
			return BaseResultVO.success();
		}
		return BaseResultVO.fail();
	}

	/**
	 * 根据Id删除
	 * id多个用逗号分隔开
	 */
	@DeleteMapping("delete/{id}")
	@ApiOperation("/根据Ids删除")
	public BaseResultVO deleteByIds(@PathVariable("id") String id) {
		if (StrUtil.isNotBlank(id)) {
			List<Long> idList = Arrays.stream(id.split(StrUtil.COMMA)).map(s -> Long.valueOf(s)).collect(Collectors.toList());
			deptService.deleteByIds(idList);
			return BaseResultVO.success();
		}
		return BaseResultVO.fail();
	}

	/**
	 * 更新数据
	 *
	 * @param deptVO
	 * @return
	 */
	@PostMapping("update")
	@ApiOperation("/更新数据")
	public BaseResultVO updateDept(@RequestBody OrmDeptVO deptVO) {
		boolean isUpdate = deptService.updateDept(deptVO);
		if (isUpdate) {
			return BaseResultVO.success();
		}
		return BaseResultVO.fail();
	}


	/**
	 * http://172.16.57.141:9800/basic_project/deptTemp/query/1
	 * 根据Id查找
	 */
	@GetMapping("query/{id}")
	@ApiOperation("/根据Id查找")
	public BaseResultVO queryById(@PathVariable("id") @Min(value = 5, message = "必须大于等于5") Long id) {
		OrmDeptVO ormDeptVO = deptService.queryDeptById(id);
		return BaseResultVO.success(ormDeptVO);
	}

	/**
	 * 查询数据 - 分页
	 */
	@GetMapping("query")
	@ApiOperation("/查询全部数据")
	public BasePageResult query() {
		List<OrmDeptVO> ormDeptVOS = deptService.queryAllDept();
		List pageList = HutoolPageUtil.toPage(1, 100, ormDeptVOS);
		return new BasePageResult(Long.valueOf(pageList.size()),1,100,pageList);
	}

	/**
	 * 根据Name查找
	 */
	@GetMapping("query/deptName/{name}")
	@ApiOperation("/根据部门名称查询")
	public BaseResultVO queryByName(@PathVariable("name") @NotNull(message = "部门名称不能为空") String deptName) {
		Long id= StringUtils.isEmpty(deptName)?Long.valueOf(1):Long.valueOf(1);
		OrmDeptVO ormDeptVO = deptService.queryDeptById(id);
		return BaseResultVO.success(ormDeptVO);
	}

	/**
	 * dept 根据查询条件查询数据
	 *
	 * @param deptCondition
	 * @return
	 */
	@ApiOperation(value = "query Dept with pageable", notes = "query Dept with pageable")
	@PostMapping("/deptPage")
	@ResponseBody
	public ResponseEntity findDeptByPage(@RequestBody OrmDeptCondition deptCondition) {
		Page<OrmDeptVO> page = deptService.findDeptByPage(deptCondition);
		if (page == null) {
			return ResponseEntity.ok(null);
		}
		PageParamVO pageParam = deptCondition.getPageParamVO();
		pageParam.setVersion(new Date());
		BasicsPageUtil.fillPageParamResult(pageParam, page);
		return ResponseEntity.ok(pageParam);
	}

}
